package wish.regularExpression;

import java.util.ArrayList;
import java.util.List;

public class OptionalElement extends RegularExpressionElement {

	private RegularExpressionElement element;

	private List<RegularExpressionElement> list;
	
	private EmptyElement empty;
	
	public OptionalElement(RegularExpressionElement element){

		this.element = element;
	
		empty = new EmptyElement();
		
		list = new ArrayList<RegularExpressionElement>();
		
		list.addAll(element.getMachingElements());
		
		list.add(new EmptyElement());
		
	}
	
	@Override
	public boolean matches(RegularExpressionElement regExpele) {
		
		if (element.matches(regExpele)){
			return true;
		}
		
		return empty.matches(regExpele);
		
	}

	@Override
	public List<RegularExpressionElement> getMachingElements() {
		
		return list;
		
	}

	
	public String toString(){
		
		return element.toString() + "?";
		
	}

	@Override
	protected RegularExpressionElement expandWithMatchingRegularExpression(
			RegularExpressionElement regExp) {
		return this;
	}

}
